/**
 * Created by eva on 2019/12/29.
 */
var can1,can2,ctx1,ctx2,canWidth,canHeight;

var lastTime,deltaTime;

var bigPic = new Image();

var mx,my;
var gameOver = false;
var ane = new Ane(),fruit = new Fruit(),mom = new Mom(),baby = new Baby(),data = new Data(),wave = new Wave(),halo = new Halo(),dust = new Dust();
document.body.onload = game;
function game(){
    init();

    lastTime = Date.now();
    deltaTime = 0;
    gameloop();


}
function init(){
    can1 = document.getElementById("canvas1");
    can2 = document.getElementById("canvas2");
    ctx1 = can1.getContext("2d");
    ctx2 = can2.getContext("2d");

    canWidth = can1.width;
    canHeight = can1.height;

    bigPic.src = "src/background.jpg";
    ane.init();
    fruit.init();
    mom.init();
    baby.init();
    wave.init();
    halo.init();
    dust.init();
    can1.addEventListener("mousemove",onMousemove,false);
    
    mx = canWidth*0.5;
    my = canHeight*0.5;


}
function gameloop(){
    window.requestAnimFrame(gameloop);
    var now = Date.now();
    deltaTime = Math.min(now - lastTime,40);
    lastTime = now;
    drawBackground();
    ane.draw();
    fruit.fruitMonitor();
    fruit.draw();
    ctx1.clearRect(0,0,canWidth,canHeight);
    mom.draw();
    baby.draw();
    if(!gameOver){
        momFruitCollision();
        momBabyCollision();
    }
    data.draw();
    wave.draw();
    halo.draw();
    dust.draw();



}
function drawBackground(){
    ctx2.drawImage(bigPic,0,0,canWidth,canHeight);
}
function onMousemove(e) {
    if(gameOver){
        return;
    }
    if(e.offsetX || e.layerX){
        mx = e.offsetX || e.layerX;
        my = e.offsetY || e.layerY;
    }
}